﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TY.UC.Http;
using TY.UC.Config;
using System.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

namespace TY.UC.Common.Extension
{
    public static class Extension
    {
        /// <summary>
        /// 转换成字符串
        /// </summary>
        public static string Bytes2Str(this IEnumerable<byte> source, string formatStr = "{0:X2}")
        {
            StringBuilder pwd = new StringBuilder();
            foreach (byte btStr in source) { pwd.AppendFormat(formatStr, btStr); }
            return pwd.ToString();
        }
        public static TyResultModel CommonMethod(this TyResultModel result, string msg, string code = "-1")
        {
            result.code = code;
            result.msg = msg;
            return result;
        }
        /// <summary>
        /// 获取图片url
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public static string GetPicStr(this string pic)
        {
            if (string.IsNullOrEmpty(pic))
                return "";
            if (pic.StartsWith("http://"))
            {
                return pic;
            }
            return "aliyunPicUrl".GetStrValue() + pic;
        }

       
        /// <summary>
        /// 获取带水印的图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public static string GetPicWatermarkStr(this string pic)
        {

            /*
        ?x-oss-process=image/resize,w_400/watermark,image_cGFuZGEucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLFBfMzA,t_90,g_se,x_10,y_10
            */

            if (string.IsNullOrEmpty(pic))
                return "";
            /*
                  http://tk-file.oss-cn-shenzhen.aliyuncs.com/product/logo.png

            panda.png?x-oss-process=image/resize,P_30
                  */

            System.Text.Encoding encode = System.Text.Encoding.ASCII;
            byte[] bytedata = encode.GetBytes("logo.png?x-oss-process=image/resize,P_20");
            string encodedObject = Convert.ToBase64String(bytedata, 0, bytedata.Length).Replace('+', '-').Replace('/', '_').TrimEnd('='); 
            if (pic.StartsWith("http://"))
            {
                return pic+ "?x-oss-process=image/watermark,image_" + encodedObject + ",t_50,g_se,x_5,y_5";
            }
            return "aliyunPicUrl".GetStrValue() + pic + "?x-oss-process=image/watermark,image_" + encodedObject + ",t_50,g_se,x_5,y_5"; 
        }



        /// <summary>
        /// 获取配置文件的AppSettings的值
        /// </summary>
        /// <param name="key">配置name名称</param>
        /// <returns>配置value值</returns>
        public static string GetStrValue(this string key)
        {
            return ConfigurationManager.AppSettings[key] ?? "";
        }
         
        /// <summary>
        /// 获取配置文件的AppSettings的值
        /// </summary>
        /// <param name="key">配置name名称</param>
        /// <returns>配置value值</returns>
        public static int GetIntValue(this string key)
        {
            int df = 0;
            if (ConfigurationManager.AppSettings[key] != null)
            {
                df = Convert.ToInt32(ConfigurationManager.AppSettings[key]);
            }
            return df;
        }
        /// <summary>
        ///json解析对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="json">json字符串</param>
        /// <returns>对象实体</returns>
        public static T JsonDe<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 对象转换为json
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>json字符串</returns>
        public static string JsonSe(this object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
        /// <summary>
        /// 对象转换为json（属性值全部为小写）
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string JsonSeLower(this object obj)
        {
            JsonSerializerSettings jss = new JsonSerializerSettings { ContractResolver = PropertyLowerCase.Instance };
            return JsonConvert.SerializeObject(obj, jss);
        }
        /// <summary>
        /// 对象转化为linqobject
        /// </summary>
        /// <param name="json">jsos字符串</param>
        /// <returns></returns>
        public static JObject JsonLinq(this string json)
        {
            return JObject.Parse(json);
        }

        /// <summary>
        /// 获取json值，key不区分大小写
        /// </summary>
        /// <param name="jobj">jobject实体</param>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public static string Value(this JObject jobj, string key)
        {
            return jobj.GetValue(key, System.StringComparison.InvariantCultureIgnoreCase) + "";
        }
    }
}